import {createRouter, createWebHistory} from "vue-router";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/app',
            name: 'app',
            component: () => import('../views/chat_main/main.vue')
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/chat_login/login.vue')
        },
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/:pathMatch(.*)*',
            // 匹配不到的路由 跳转到 404页面
            name: 'NotFound',
            component: () => import('../views/404/404.vue')
        }
    ]
})

// 设置全局路由守卫，当从/app路由跳转到别的路由时，清除sessionStore中的用户信息userInfo
router.beforeEach((to, from) => {
    if (from.path === '/app') {
        sessionStorage.removeItem('userInfo')
    }
})

export default router